package com.xcdh.domain.vo;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcdh.consts.StateCode;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
 * @author 王景光
 */
public class R<T> implements Serializable {
    private final static String MSG_OK = "操作成功" ;
    private final static String MSG_ERROR = "服务器异常" ;
    private final static String TOTAL = "total" ;
    private final static String ROWS = "rows" ;

    private T data ;
    private Integer code ;
    private Boolean status ;
    private String msg ;

    public R() {
        this(StateCode.OK , MSG_OK , true);
    }
    public R( String msg , T data) {
        this(StateCode.OK , msg , true , data );

    }
    public R(Integer code , String msg , boolean status) {
        this(code , msg ,status , null ) ;
    }
    public R(Integer code , String msg , boolean status , T data) {
        this.code = code ;
        this.msg = msg ;
        this.status = status ;
        this.data = data ;
    }

    public static <T> R<T> error() {
        return error(StateCode.ERROR, MSG_ERROR , null);
    }

    public static <T> R<T> error(String msg) {
        return error(StateCode.ERROR, msg , null);
    }

    public static <T> R<T> error(T map) {
        return error(StateCode.ERROR, MSG_ERROR , map);
    }
    public static <T> R<T> error(int code, String msg ) {
        return new R<>(code , msg , false , null);
    }
    public static <T> R<T> error(int code, String msg , T data) {
        return new R<>(code , msg , false , data);
    }

    public static <T> R<T> ok(String msg) {
        return new R<>(StateCode.OK , msg , true);
    }

    public static <T> R<T> okData(T data) {
        return new R<>(MSG_OK , data);
    }

    public static <T> R<T> ok(T data , String msg) {
        return new R<>(msg , data);
    }




    public static <T> R<T> ok() {
        return new R<>();
    }

    public static <T> R<T> noLogin() {
        return noLogin(null);
    }

    public static <T> R<T> noLogin(String msg) {
        return new R<>(StateCode.UNAUTHORIZED , nvlString(msg,  "登录信息过期，请重新登录") , false);
    }
    public static String nvlString(String target , String spare){
        return StringUtils.isBlank(target) ? spare : target ;
    }

    /**
     * status > 0 ? 成功 : 失败
     *
     * @param status
     * @return
     */
    public static <T> R<T> result(int status) {
        return status > 0 ? R.ok() : R.error();
    }
    public static <T> R<T> result(boolean status) {
        return status  ? R.ok() : R.error();
    }

    public static <T> R<PageVO<T>> page(IPage<T> page) {
        return R.ok(new PageVO<>(page.getTotal() ,page.getRecords()), MSG_OK);
    }

    public static  <T> R<PageVO<T>> page(long total, Collection<T> rows) {
        return R.ok(new PageVO<>(total ,rows), MSG_OK);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
